package com.zwb.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwb.service.dto.AdminRoleDto;
import com.zwb.service.mapper.AdminRoleMapper;
import com.zwb.service.pojo.Admin;
import com.zwb.service.pojo.AdminRole;
import com.zwb.service.service.IAdminRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zwb
 * @since 2022-08-23
 */
@Service
public class AdminRoleServiceImpl extends ServiceImpl<AdminRoleMapper, AdminRole> implements IAdminRoleService {


    /**
     * 更新操作员角色信息
     *
     * @param adminRoleDto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAdminRoles(AdminRoleDto adminRoleDto) {

        /* 删除原来的 */
        this.remove(new LambdaQueryWrapper<AdminRole>()
                .eq(AdminRole::getAdminId, adminRoleDto.getAdminId()));

        /* 新增 */
        List<AdminRole> collect = adminRoleDto.getRoles().stream().map(item -> {
            AdminRole adminRole = new AdminRole();
            adminRole.setRid(item);
            adminRole.setAdminId(adminRoleDto.getAdminId());
            return adminRole;
        }).collect(Collectors.toList());
        this.saveBatch(collect);


    }
}
